package cn.zhangjie.test;

import cn.zhangjie.util.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.util.HashMap;

public class EstEST {
    @Test
    public void testadd(){
        TransportClient client = ESClientUtil.getClient();
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",1l);
        map.put("name","古巨基");
        map.put("age",1);
        IndexRequestBuilder requestBuilder = client.prepareIndex("pethome", "pet", "1").setSource(map);
        System.out.println(requestBuilder.get());
        client.close();

    }
    @Test
    public void testget(){
        TransportClient client = ESClientUtil.getClient();
        GetRequestBuilder getRequestBuilder = client.prepareGet("pethome", "pet", "1");
        System.out.println(getRequestBuilder.get().getSource());
        client.close();
    }
    @Test
    public void testupdate(){
        TransportClient client = ESClientUtil.getClient();

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "我在源码时代学习Java!我爱Java!");
        UpdateResponse updateResponse = client.prepareUpdate("pethome", "pet", "1").setDoc(map).get();
        System.out.println(updateResponse);
        client.close();
    }
    @Test
    public void testdel(){
        TransportClient client = ESClientUtil.getClient();
        client.prepareDelete("pethome", "pet", "1").get();
        client.close();
    }


    //批量添加数据
    @Test
    public void testBatch() throws Exception {
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 3.批量创建文档，放入批量操作对象中
        for (int i = 2; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "user", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "我在源码学习Java!" + i)
                            .field("age", i - 1)
                            .field("sex", i%2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 4.关闭连接
        client.close();
    }

    @Test
    public void testpatchdel(){
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 查询条件：- 查询用户表，name包含：我在源码，age在1~12之间，每页大小2，从二页开始查，按照age倒序
        // 2.得到搜索对象
        SearchRequestBuilder builder = client.prepareSearch("pethome");
        // 3.指定要搜索的类型
        builder.setTypes("user");
        // 4.指定Query搜索对象，当参数是接口时我们可以传递：接口的工具类、接口实现、匿名内部类，此处传递接口工具类
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.matchQuery("name","我在源码"));

        boolQuery.filter(QueryBuilders.rangeQuery("age").gte(1).lte(12)).filter(QueryBuilders.termQuery("sex",1));

        builder.setSize(3);
        builder.setFrom(3);
        builder.addSort("age", SortOrder.DESC);

        SearchHit[] hits = builder.get().getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit);
        }
        // 4.关闭连接
        client.close();

    }


}
